import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;


public class SettingsMenu extends ScreenManager{

	private static final long serialVersionUID = 1L;
	private JButton backMenu;
	private JPanel settingsMenuPanel;
	private String userName;
	private EventListenerList listenerList = new EventListenerList();

	public SettingsMenu(String name){
		userName = name;
		
		ImageIcon backBg = new ImageIcon(getClass().getResource("bgs/backBg.jpg"));

		JTabbedPane jtbExample = new JTabbedPane();
		JPanel jplInnerPanel1 = createBirdInnerPanel();
		jtbExample.addTab("Bird",jplInnerPanel1);
		jtbExample.setSelectedIndex(0);
		JPanel jplInnerPanel2 = createObstacleInnerPanel();
		jtbExample.addTab("Obstacle",jplInnerPanel2);
		JPanel jplInnerPanel3 = createBonusInnerPanel();
		jtbExample.addTab("Bonus",jplInnerPanel3);
		JPanel jplInnerPanel4 = createSoundInnerPanel();
		jtbExample.addTab("Sound", jplInnerPanel4);

		// Add the tabbed pane to this panel.
		setLayout(new GridLayout(1, 1));
		add(jtbExample);
		
		backMenu = new JButton("Back to Main Menu", backBg);
		backMenu.setBounds(130, 310, 95, 55);
		jtbExample.setBounds(0, 0, 390, 300);
		settingsMenuPanel = new JPanel();
		settingsMenuPanel.setLayout(null);
		settingsMenuPanel.add(jtbExample);
		settingsMenuPanel.add(backMenu);
		getContentPane().add(settingsMenuPanel);


		backMenu.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				closeTab();
				MainMenu mainMenu = new MainMenu(userName);
				mainMenu.setVisible(true);
			}
		});

	}
	
	protected JPanel createBirdInnerPanel() {
		JPanel jplPanel = new JPanel();
		final JComboBox<String> birdImageBox = new JComboBox<String>();
		firePlayerEvent(new SettingsManager( this, "fillBirds", userName, birdImageBox ));
		jplPanel.setLayout(null);
		JButton addBird = new JButton("Add Bird");
		addBird.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	firePlayerEvent(new SettingsManager( this, "addBird", userName, birdImageBox ));
		    }
		});
		JButton changeBird = new JButton("Change Bird");
		changeBird.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	firePlayerEvent(new SettingsManager( this, "changeBird", userName, birdImageBox  ));
		    }
		});
		addBird.setBounds(30, 30, 150, 30);
		birdImageBox.setBounds(30, 80, 150, 30);
		changeBird.setBounds(30, 130, 150, 30);
		jplPanel.add(birdImageBox);
		jplPanel.add(addBird);
		jplPanel.add(changeBird);
		
		return jplPanel;
	}
	
	protected JPanel createObstacleInnerPanel() {
		final JPanel jplPanel = new JPanel();
		final JComboBox<String> obstacleImageBox = new JComboBox<String>();
		firePlayerEvent(new SettingsManager( this, "fillObstacles", userName, obstacleImageBox ));
		jplPanel.setLayout(null);
		JButton addObstacle = new JButton("Add Obstacle");
		addObstacle.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	firePlayerEvent(new SettingsManager( this, "addObstacle", userName, obstacleImageBox));
		    }
		});
		JButton changeObstacle = new JButton("Change Obstacle");
		changeObstacle.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	firePlayerEvent(new SettingsManager( this, "changeObstacle", userName, obstacleImageBox ));
		    }
		});
		addObstacle.setBounds(30, 30, 150, 30);
		obstacleImageBox.setBounds(30, 80, 150, 30);
		changeObstacle.setBounds(30, 130, 150, 30);
		jplPanel.add(obstacleImageBox);
		jplPanel.add(addObstacle);
		jplPanel.add(changeObstacle);
		
		return jplPanel;
	}
	protected JPanel createBonusInnerPanel() {
		JPanel jplPanel = new JPanel();
		final JComboBox<String> bonusImageBox = new JComboBox<String>();
		firePlayerEvent(new SettingsManager( this, "fillBonuses", userName, bonusImageBox ));
		jplPanel.setLayout(null);
		JButton addBonus = new JButton("Add Bonus");
		addBonus.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	firePlayerEvent(new SettingsManager( this, "addBonus", userName, bonusImageBox ));
		    }
		});
		JButton changeBonus = new JButton("Change Bonus");
		changeBonus.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	firePlayerEvent(new SettingsManager( this, "changeBonus", userName, bonusImageBox ));
		    }
		});
		addBonus.setBounds(30, 30, 150, 30);
		bonusImageBox.setBounds(30, 80, 150, 30);
		changeBonus.setBounds(30, 130, 150, 30);
		jplPanel.add(bonusImageBox);
		jplPanel.add(addBonus);
		jplPanel.add(changeBonus);
		
		return jplPanel;
	}
	protected JPanel createSoundInnerPanel() {
		JPanel jplPanel = new JPanel();
		final JComboBox<String> songBox = new JComboBox<String>();
		firePlayerEvent(new SoundManager( this, "fillSound", userName, songBox ));
		jplPanel.setLayout(null);
		JButton addSound = new JButton("Add Sound");
		addSound.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	firePlayerEvent(new SoundManager( this, "addSound", userName, songBox ));
		    }
		});
		JButton changeSound = new JButton("Play");
		changeSound.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	firePlayerEvent(new SoundManager( this, "changeSound", userName, songBox ));
		    }
		});
		
		final JSlider sliderVol = new JSlider(JSlider.HORIZONTAL, 1, 100, 50);
		sliderVol.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e){
				float val = sliderVol.getValue()/100f;
				firePlayerEvent(new SoundManager( this, "volumeMp3", val));
			}
		});
		JButton stopSound = new JButton("Stop");
		stopSound.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	firePlayerEvent(new SoundManager( this, "stopMp3"));
		    }
		});
		
		
		addSound.setBounds(30, 30, 150, 30);
		songBox.setBounds(30, 80, 150, 30);
		changeSound.setBounds(30, 130, 75, 30);
		stopSound.setBounds(105,130,75,30);
		sliderVol.setBounds(30, 180, 150,30);
		
		jplPanel.add(songBox);
		jplPanel.add(addSound);
		jplPanel.add(changeSound);
		jplPanel.add(stopSound);
		jplPanel.add(sliderVol);
		
		return jplPanel;
	}

	private void closeTab(){
		this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING));
	}
	public void firePlayerEvent(SettingsManager event)
	{
		Object[] listeners = listenerList.getListenerList();
		
		for ( int i = 0 ; i < listeners.length; i += 2 )
		{
			if (listeners[i] == SoundListener.class)
			{
				((SettingsListener)listeners[i + 1]).playerEventOccurred(event);
			}
		}	
	}
	public void firePlayerEvent(SoundManager event)
	{
		Object[] listeners = listenerList.getListenerList();
		
		for ( int i = 0 ; i < listeners.length; i += 2 )
		{
			if (listeners[i] == SoundListener.class)
			{
				((SoundListener)listeners[i + 1]).playerEventOccurred(event);
			}
		}	
	}

}
